package com.qqbot.impl.interceptor;


import com.qqbot.impl.exception.BException;
import com.qqbot.impl.exception.code.BaseResponseCode;
import com.qqbot.impl.utils.SignMD5Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.Instant;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定义拦截器接口验签使用
 *
 * @author changwenpeng
 * @date 2022年5月28日
 */
public class SignatureInterceptor implements HandlerInterceptor {

    @Value("${app.open.api.secret}")
    private String secret;
    @Value("#{'${app.open.api.appIdList}'.split(',')}")
    private List<String> appIdList;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) {
//        String appId = request.getParameter("appId");
//        if (!appIdList.contains(appId)) {
//            throw new BException(BaseResponseCode.APPID_ERROR.getCode(), BaseResponseCode.APPID_ERROR.getMsg());
//        }
//        String sign = request.getParameter("sign");
//        if (StringUtils.isEmpty(sign)) {
//            throw new BException(BaseResponseCode.SIGN_ERROR.getCode(), BaseResponseCode.SIGN_ERROR.getMsg());
//        }
//        String timestamp = request.getParameter("timestamp");
//        // 时间戳要为10位的秒级时间戳
//        if (timestamp.length() != 10) {
//            throw new BException(BaseResponseCode.TIMESTAMP_ERROR.getCode(), BaseResponseCode.TIMESTAMP_ERROR.getMsg());
//        };
//        //check时间戳的值是否在当前时间戳前后一30秒以内
//        String currTimestamp = String.valueOf(Instant.now().getEpochSecond());
//        int currTimestampNum = Integer.parseInt(currTimestamp);
//        int verifyTimestampNum = 0;
//        try {
//            verifyTimestampNum =  Integer.parseInt(timestamp);
//        } catch (NumberFormatException e) {
//            throw new BException(BaseResponseCode.TIMESTAMP_ERROR.getCode(), BaseResponseCode.TIMESTAMP_ERROR.getMsg());
//        }
//        // 在30秒范围之外，访问已过期
//        if (Math.abs(verifyTimestampNum - currTimestampNum) > 30) {
//            throw new BException(BaseResponseCode.SIGN_TIMEOUT_ERROR.getCode(), BaseResponseCode.SIGN_TIMEOUT_ERROR.getMsg());
//        }
//
//        Enumeration<?> pNames = request.getParameterNames();
//        Map<String, String> params = new HashMap<>();
//        while (pNames.hasMoreElements()) {
//            String pName = (String) pNames.nextElement();
//            if ("sign".equals(pName)) {
//                continue;
//            }
//            String pValue = request.getParameter(pName);
//            params.put(pName, pValue);
//        }
//        if (sign.equals(SignMD5Utils.getSign(params, secret))) {
//            return true;
//        } else {
//            throw new BException(BaseResponseCode.SIGN_ERROR.getCode(), BaseResponseCode.SIGN_ERROR.getMsg());
//        }
        return true;
    }
}